package com.magold.physics.magnetic;

import org.andengine.extension.physics.box2d.util.constants.PhysicsConstants;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class BaseMagneticAffectable implements MagnetAffectable {
	private Body mBody;
	private float[] mBuffer = new float[2];
	private DirectionSolver mDirectionSolver;
	
	public BaseMagneticAffectable(Body pBody, DirectionSolver pDirectionSolver) {
		setBody(pBody);
		mDirectionSolver = pDirectionSolver;
	}
	
	public void setBody(Body pBody) {
		mBody = pBody;
	}
	
	@Override
	public void onAffected(MagneticPole pPole, float pX, float pY, float pForce) {
		Vector2 direc = mDirectionSolver.computeDirection(pPole, pX, pY);
		Vector2 center = mBody.getWorldCenter();
		mBody.applyForce(direc.x*pForce, direc.y*pForce, center.x, center.y);
	}

	@Override
	public float[] getMagneticAffectPosition() {
		Vector2 position = mBody.getPosition();
		mBuffer[0] = position.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
		mBuffer[1] = position.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
		return mBuffer;
	}

}
